<?php 
namespace Admin\Controller;
/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PriceController extends AdminController{
	public function index(){
		$name       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($name)){
            $map['id|name']=   array(intval($name),array('like','%'.$name.'%'),'_multi'=>true);
        }else{
            $map['name']    =   array('like', '%'.(string)$name.'%');
        }
        $list   = $this->lists('price', $map);
        int_to_string($list);
foreach ($list as $k => $va) {
          $strval = ''; 
          $goods[] = M('goods')->where(array('id'=>$va['gid']))->field('id,name')->find();
          $where['id'] = array('in',$va['attrvalid']);             
          $where['gid'] = $va['gid'];
          $attr = M('attrval')->where($where)->field('id,value')->select();
          $list[$k]['gname']=$goods[$k]['name'];
         
                         
       foreach($attr as $val){          
            $strval .= $val['value'].'、';          
          }
        $strval = rtrim($strval,'、');      
        $attrval = array('value'=>$strval); 
        $list[$k]['attrname']=$attrval['value'];
    }      
       
        // var_dump($strval);
        // var_dump($attrval);
       
        // $this->assign('attrkey',$attrkey);
        $this->assign('res', $list);
        $this->meta_title = '价格列表';
        $this->display();
    } 

public function price(){
                

        if(IS_POST){       
            $price = D('price');
            foreach (I('post.tprice') as $k => $v) {
                $data['gid'] = I('post.gid');
                $data['tprice'] = $v;
                $data['mprice'] = I('post.mprice')[$k];
                $data['num'] = I('post.num')[$k];
                $data['attrvalid'] = I('post.attrvalid')[$k];
                
                $res = $price->create($data); 
                    if($res){
                    if($price->add($res)){
                    $this->success('定制价格成功!');

            }else{
                    $this->error('定制价格失败');

            }
                }else{
                    $this->error($price->getError());

                }
            }
        
        }else{
                $gid = I('get.id');
                $attr = M('attrval')->where(array('gid'=>$gid))->select();              
                $goods = M('goods')->field('id,name')->find($gid);
                // var_dump($attr);
                  if($attr){
                    $tmpint = $attr[0]['kid'];
                    // var_dump($tmpint);
                    foreach ($attr as $k => $v) {
                      if($v['kid'] == $tmpint){
                        // $tmparr[$v['kid']][$v['id']] = $v['value'];
                        $attrvalid[$v['kid']][$v['id']] = $v['id'];
                        
                      }else{
                        $tmpint = $v['kid'];
                        // $tmparr[$v['kid']][$v['id']] = $v['value'];
                        $attrvalid[$v['kid']][$v['id']] = $v['id'];
                      }
                    }
                  } 
                
                  
                  // $res = $this->dikaer(array_values($tmparr));
                  // var_dump($res);
                  $attrvalid = $this->dikaer(array_values($attrvalid));
                  
                  foreach ($attrvalid as &$v) {
                      $arr = explode(',',$v);
                      sort($arr);
                      $v = implode(',',$arr);
                  }

                  // var_dump($attrvalid);
                  // 去掉已定制价格  
                   $price = M('price')->field('attrvalid')->where(array('gid'=>$gid))->select();
                 
                $price = array_column($price, 'attrvalid');//二维数组变一维数组

                 if(!empty($price)){
         $attrid = array_diff($attrvalid,$price); 
         // var_dump($attrid);     
             }else{
           $attrid = $attrvalid;
       }     
        foreach ($attrid as $key => $val) {
            
             $strval = '';
             $attr = array();
             $where['id'] = array('in',$val); 
             $where['gid'] = $gid;
             // var_dump($where);
            $attr = M('attrval')->where($where)->select();   
            foreach ($attr as $valu) {

                $strval .= $valu['value'].'、';        
          }

        $strval = rtrim($strval,'、');
        $attrval[] = array('value'=>$strval);
        // var_dump($attrval);
        

    } 

    // var_dump($attrvalid);

                    $this->assign('attrvalid',$attrid);
                    $this->assign('res',$attrval);
                    $this->assign('goods',$goods);
                    $this->display();    
        }
    }
 public function dikaer($sets){
    // 保存结果
    $result = array();
    // 循环遍历集合数据
    $count = count($sets);
    // var_dump($count);
    if($count == 1){

        $result = $sets[0];

    }else{

    for($i=0; $i<$count-1; $i++){
        // 初始化
        if($i==0){

            $result = $sets[$i];
            // var_dump($result);

        }

        // 保存临时数据
        $tmp = array();
        // 结果与下一个集合计算笛卡尔积
        foreach($result as $res){
            foreach($sets[$i+1] as $set){
                $tmp[] = $res.','.$set;
               
            }
        }

        // 将笛卡尔积写入结果
         $result = $tmp;
    }

    }
    return $result;

}
// public function edit(){
//     if (IS_POST) {
//         $price = D('price');
//             $data = $price->create();
//             if($data){
//                 if($price->save()!== false){
//                     $this->success('修改成功');
//                 } else {
//                     $this->error('修改失败');
//                 }
//             } else {
//                 $this->error($price->getError());
//             }
//     }else{
//         $price=M('price');
//         $res=$price->
//     }
// }


	}

 ?>
